<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/3/15
 * Time: 16:29
 */

namespace Api\Controller;

Class FeedbackController extends BaseController{

    /**
     * 客户反馈
     */
    public function index(){
        //解决跨域
        echoHeader();
        $content = I('post.content','');
        $user_id = I('user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $phone = I('post.phone','');
        if($content=='' || $user_id ==''){
            $array = array("err_code"=>403,'err_msg'=>'参数缺失！');
            $this->ajaxReturn($array);
        }
        $data['content'] = $content;
        $data['contact'] = $phone;
        $data['user_id'] = $user_id;
        $data['add_time'] = time();
        $info = M('user_advise')->add($data);
        if($info){
            $array = array("err_code"=>200,'err_msg'=>'提交成功！');
            $this->ajaxReturn($array);
        }else {
            $array = array("err_code" => 403, 'err_msg' => '提交失败！');
            $this->ajaxReturn($array);
        }

    }

    /**
     * 客户投诉
     * @param order_id 订单号
     * @param user_id 用户id
     * @param reason 投诉原因
     * @param content 投诉内容
     */
    public function Complaint(){
        $order_id = I('post.order_id'); //对外订单号
        $user_id = I('post.user_id'); //用户id
        $reason = I('post.reason'); //订单原因
        $content = I('post.content',''); //内容
        if(!$order_id || !$user_id || !$reason){
            $array = array('err_code'=>404,'err_msg'=>'参数缺失');
            $this->ajaxReturn($array);
        }
        $orderExist = M('repair_order')->field('repair_order_id')->where("repair_order_num_alias = $order_id")->find();
        if(!$orderExist) {
            $info = array('code'=>'404','msg'=>'订单不存在！');
            $this->ajaxReturn($info);
        }
        $repair_order_id =  $orderExist[repair_order_id];
        $maps['order_id'] = array('eq',$repair_order_id);
        $maps['user_id'] = array('eq',$user_id);
        $UserResult = M('repair_order_complaint')->where($maps)->find();
        if($UserResult){
            $array = array('err_code'=>202,'err_msg'=>'您已投诉过该订单！');
            $this->ajaxReturn($array);
        }else{
            $save['order_id'] = $repair_order_id;
            $save['user_id'] = $user_id;
            $save['reason'] = $reason;
            $save['content'] = $content;
            $save['add_time'] = time();
            $addResult = M('repair_order_complaint')->add($save);
            if($addResult){
                $wheOrder['repair_order_num_alias'] = $order_id;
                $updOrder['is_complaint'] = 1;
                M('repair_order')->where($wheOrder)->save($updOrder);
                $array = array('err_code'=>200,'err_msg'=>'投诉成功');
                $this->ajaxReturn($array);
            }else{
                $array = array('err_code'=>403,'err_msg'=>'投诉失败');
                $this->ajaxReturn($array);
            }
        }

    }
}